package com.hsgene.common.util.support;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页支持
 *
 * @author wangbing
 * @version 1.0, 2018/7/18
 */
public class Pagination<T> implements Serializable {

    private static final long serialVersionUID = 3505362118495881042L;

    /**
     * 总量
     */
    private int totalCount;

    /**
     * 当前页数
     */
    private int currentPage;

    /**
     * 总页数
     */
    private int totalPage;

    /**
     * 分页查询结果
     */
    private List<T> result = new ArrayList<>();

    public Pagination() {
    }

    public Pagination(int totalCount, int currentPage, int pageSize, List<T> result) {
        this.totalCount = totalCount;
        this.currentPage = currentPage;
        this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        this.result = result;
    }

    public Pagination(int totalCount, LimitHelper limiter, List<T> result) {
        this(totalCount, limiter.getPage(), limiter.getSize(), result);
    }

    /**
     * 根据分页参数和数据总量创建一个Pagination实例.
     *
     * @param limitHelper 分页参数组件
     * @param totalCount  数据总量
     * @return Pagination实例
     */
    public static <T> Pagination<T> newInstance(LimitHelper limitHelper, int totalCount) {
        return newInstance(limitHelper, totalCount, null);
    }

    /**
     * 根据分页参数和数据总量创建一个Pagination实例.
     *
     * @param limitHelper 分页参数组件
     * @param totalCount  数据总量
     * @param result      分页结果集
     * @return Pagination实例
     */
    public static <T> Pagination<T> newInstance(LimitHelper limitHelper, int totalCount, List<T> result) {
        return new Pagination<>(totalCount, limitHelper, result);
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public List<T> getResult() {
        return result;
    }

    public void setResult(List<T> result) {
        this.result = result;
    }
}
